<template>
  <div>
    <el-dialog
      :title="createDialogObj.title"
      width="45%"
      :visible.sync="createDialogObj.dialogVisible"
      @closed="
        () => {
          this.$refs.formRef.resetFields();
        }
      "
    >
      <el-form
        :model="createObj"
        label-width="110px"
        ref="formRef"
        :rules="rules"
      >
        <el-row>
          <el-col :span="12"
            ><el-form-item label="申请类型" prop="type">
              <el-select v-model="createObj.type" placeholder="请选择">
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="投入品" prop="supplyName">
              <el-select
                @change="
                  (index) => {
                    this.createObj.supplyName = this.ShopSupplyInfo[index].name;
                    this.createObj.supplyType = this.ShopSupplyInfo[index].type;
                    this.createObj.supplyId = this.ShopSupplyInfo[index].id;
                    this.createObj.supplier = this.ShopSupplyInfo[
                      index
                    ].supplierName;
                    this.createObj.specification = this.ShopSupplyInfo[
                      index
                    ].specification;
                    this.createObj.skuNumber = this.ShopSupplyInfo[
                      index
                    ].skuNumber;
                    this.searchSupply(this.createObj.skuNumber);
                  }
                "
                v-model="createObj.supplyName"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in ShopSupplyInfo"
                  :key="item.id"
                  :label="item.name"
                  :value="index"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item label="数量" prop="amount">
              <el-input-number
                controls-position="right"
                :min="0"
                v-model="createObj.amount"
              ></el-input-number> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="投入品规格">
              <el-input disabled v-model="createObj.specification"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="基地仓库" prop="warehouseId">
              <el-select
                v-model="baseId"
                placeholder="请选择基地"
                @change="handle"
              >
                <el-option
                  v-for="item in baseList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option> </el-select></el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-select
              v-model="warehouseName"
              placeholder="请选择仓库"
              @change="chooseWareHouse"
            >
              <el-option
                v-for="(item, index) in wareHouseList"
                :key="item.id"
                :label="item.name"
                :value="index"
              >
              </el-option> </el-select
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item label="购买日期" prop="purchaseTime">
              <el-date-picker
                type="datetime"
                v-model="createObj.purchaseTime"
                placeholder="选择时间"
                value-format="yyyy-MM-mm HH:mm:ss"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="12" v-show="createObj.type === 1">
            <el-form-item label="操作日期" prop="operationTime">
              <el-date-picker
                type="datetime"
                v-model="createObj.operationTime"
                placeholder="选择时间"
                value-format="yyyy-MM-mm HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="图片证明">
          <el-upload
            class="upload-demo"
            drag
            :action="actionUrl"
            :on-success="upLoad"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input type="textarea" v-model="createObj.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="
            () => {
              this.$emit('closeDialog');
            }
          "
          >取 消</el-button
        >
        <el-button v-auth="'breed_input_access_insert'" type="primary" @click="createApplicationEvent"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import {searchBySku} from '../../../Api/productionmanage/index'
import { getAllWarehouseInfo } from "../../../Api/productionmanage/Supply_warehouse";
import { createApplication } from "../../../Api/productionmanage/applicantAndManage";
export default {
  props: {
    createDialogObj: {
      type: Object,
    },
  },
  data() {
    return {
      actionUrl: "http://120.77.156.205:9610/file/upload",
      createObj: {
        type: null,
        managerId: null,
      },

      rules: {
        managerId: [
          { required: true, message: "请选择申请人", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择申请类型", trigger: "blur" }],
        supplyName: [
          { required: true, message: "请选择投入品", trigger: "blur" },
        ],
        amount: [{ required: true, message: "请选择进货量", trigger: "blur" }],
        operationTime: [
          { required: true, message: "请选择操作日期", trigger: "blur" },
        ],
        warehouseId: [
          { required: true, message: "请选择仓库", trigger: "blur" },
        ],
        purchaseTime: [
          { required: true, message: "请选择购买日期", trigger: "blur" },
        ],
      },
      wareHouseList: [],
      baseId: null,
      warehouseName: "",
    };
  },
  computed: {
    ...mapState({
      staffInfoList: (state) => state.roleInfo.staffInfo, //企业所有角色信息
      ShopSupplyInfo: (state) => state.productionInfo.ShopSupplyInfo,
      baseList: (state) => state.productionInfo.baseInfoList, //企业基地
      specificationList: (state) => state.productionInfo.specificationList,
      typeList: (state) => state.productionInfo.typeList,
    }),
  },
  created() {
    this.getWareHouseInfoList();
  },
  methods: {
    async searchSupply(id){
      const {data: res} = await searchBySku(id);
      console.log(res);
    },
    async getWareHouseInfoList() {
      const { data: res } = await getAllWarehouseInfo(this.baseId);
      this.wareHouseList = res.data;
    },
    async createApplicationEvent() {
      console.log(this.createObj);
      // this.$refs.formRef.validateField()
      const { data: res } = await createApplication(this.createObj);
      console.log(res);
      if (res.statusCode === 20000) {
        this.$message.success("申请成功");
        this.$emit("closeDialog");
        this.$emit("getTableData");
      }
    },
    upLoad(res) {
      let pic = JSON.parse(res.data);
      this.createObj.picture = pic.url;
    },
    handle(id) {
      id--;
      this.baseId = this.baseList[id].id;
      this.getWareHouseInfoList();
    },
    chooseWareHouse(index) {
      this.warehouseName = this.wareHouseList[index].name;
      this.createObj.warehouseId = this.wareHouseList[index].id;
      this.createObj.managerId = this.wareHouseList[index].managerId;
    },
  },
};
</script>

<style>
</style>